package com.a3dweijing.wetouch2.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.Common.RefreshRecycler.CommonrRecyclerAdapter;
import com.a3dweijing.wetouch2.Common.RefreshRecycler.ViewHolder;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.GlideUtil;
import com.a3dweijing.wetouch2.bean.ShoppingHome;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/22.
 */

public class ShoppingHomeAdapter extends CommonrRecyclerAdapter
{
    // 所有监听的回调
    private CallBack callBack;
    
    public void setOnClick(CallBack callBack)
    {
        this.callBack = callBack;
    }
    
    public ShoppingHomeAdapter(Context context, List datas)
    {
        super(context, datas);
    }
    
    @Override
    public void convert(RecyclerView.ViewHolder holder, final Object o)
    {
        switch (((ViewHolder)holder).flag)
        {
            case 0:
                ArrayList<ShoppingHome.BannerBean> bannerBeans = (ArrayList<ShoppingHome.BannerBean>)o;
                ArrayList<String> url = new ArrayList();
                for (ShoppingHome.BannerBean bannerBean : bannerBeans)
                {
                    url.add(bannerBean.getS_banner_imgUrl());
                }
                Banner banner = ((ViewHolder)holder).getView(R.id.banner);
                // 设置图片加载器
                banner.setImageLoader(new GlideImageLoader());
                // 设置图片集合
                banner.setImages(url);
                // banner设置方法全部调用完毕时最后调用
                banner.start();
                break;
            case 1:
                ArrayList<ShoppingHome.ClassifyBean> classifyBeans = (ArrayList<ShoppingHome.ClassifyBean>)o;
                for (int i = 0; i < classifyBeans.size(); i++)
                {
                    switch (i)
                    {
                        case 0:
                            final RelativeLayout rl = ((ViewHolder)holder).getView(R.id.rl_baojianping);
                            rl.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view)
                                {
                                    callBack.doNext(rl.getId(), o);
                                }
                            });
                            ImageView view = ((ViewHolder)holder).getView(R.id.imagebuom1);
                            GlideUtil.getInstance()
                                .showImage(mContext, classifyBeans.get(i).getGoods_type_img_url(), view);
                            TextView textView = ((ViewHolder)holder).getView(R.id.te_button1);
                            textView.setText(classifyBeans.get(i).getGoods_type_name());
                            break;
                        case 1:
                            final RelativeLayout rl2 = ((ViewHolder)holder).getView(R.id.rl_jiankangshiping);
                            rl2.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view)
                                {
                                    callBack.doNext(rl2.getId(), o);
                                }
                            });
                            ImageView view2 = ((ViewHolder)holder).getView(R.id.imagebuom2);
                            GlideUtil.getInstance()
                                .showImage(mContext, classifyBeans.get(i).getGoods_type_img_url(), view2);
                            TextView textView2 = ((ViewHolder)holder).getView(R.id.te_button2);
                            textView2.setText(classifyBeans.get(i).getGoods_type_name());
                            break;
                        case 2:
                            final RelativeLayout rl3 = ((ViewHolder)holder).getView(R.id.jiankangjixie);
                            rl3.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view)
                                {
                                    callBack.doNext(rl3.getId(), o);
                                }
                            });
                            ImageView view3 = ((ViewHolder)holder).getView(R.id.imagebuom3);
                            GlideUtil.getInstance()
                                .showImage(mContext, classifyBeans.get(i).getGoods_type_img_url(), view3);
                            TextView textView3 = ((ViewHolder)holder).getView(R.id.te_button3);
                            textView3.setText(classifyBeans.get(i).getGoods_type_name());
                            break;
                        case 3:
                            final RelativeLayout rl4 = ((ViewHolder)holder).getView(R.id.jiankangtuangou_rl);
                            rl4.setOnClickListener(new View.OnClickListener()
                            {
                                @Override
                                public void onClick(View view)
                                {
                                    callBack.doNext(rl4.getId(), o);
                                }
                            });
                            ImageView view4 = ((ViewHolder)holder).getView(R.id.imagebuom4);
                            GlideUtil.getInstance()
                                    .showImage(mContext, classifyBeans.get(i).getGoods_type_img_url(), view4);
                            TextView textView4 = ((ViewHolder)holder).getView(R.id.te_button4);
                            textView4.setText(classifyBeans.get(i).getGoods_type_name());
                            break;
                    }
                }
                break;
            case 2:
                ImageView view = ((ViewHolder)holder).getView(R.id.shopping_cen_img);
                TextView tvMore = ((ViewHolder)holder).getView(R.id.text_more);
                ArrayList<ShoppingHome.BigPictureRecommendBean> bigPictureRecommendBeans =
                    (ArrayList<ShoppingHome.BigPictureRecommendBean>)o;
                if (bigPictureRecommendBeans != null && bigPictureRecommendBeans.size() > 0)
                {
                    ShoppingHome.BigPictureRecommendBean bigPictureRecommendBean = bigPictureRecommendBeans.get(0);
                    GlideUtil.getInstance().showImage(mContext, bigPictureRecommendBean.getS_goods_img_url(), view);
                    tvMore.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                    tvMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            callBack.doNext(view.getId(), o);
                        }
                    });
                    view.setOnClickListener(new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            callBack.doNext(view.getId(), o);
                        }
                    });
                }
                break;
            case 3:
                RelativeLayout relativeLayout = ((ViewHolder)holder).getView(R.id.rl_item_shopping);
                relativeLayout.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        callBack.doNext(view.getId(), o);
                    }
                });
                ImageView itemImg = ((ViewHolder)holder).getView(R.id.item_img);
                ShoppingHome.HomeRecommendBean homeRecommendBean = (ShoppingHome.HomeRecommendBean)o;
                String s_goods_img_url = homeRecommendBean.getS_goods_img_url();
                GlideUtil.getInstance().showImage(mContext, s_goods_img_url, itemImg);
                TextView textViewname = ((ViewHolder)holder).getView(R.id.item_name);
                TextView item_describe = ((ViewHolder)holder).getView(R.id.item_describe);
                TextView item_price = ((ViewHolder)holder).getView(R.id.item_price);
                TextView originalPrice = ((ViewHolder)holder).getView(R.id.original_price);
                textViewname.setText(homeRecommendBean.getS_goods_name());
                item_describe.setText(homeRecommendBean.getS_goods_describe());
                item_price.setText("￥" + homeRecommendBean.getS_price());
                originalPrice.setText("原价:" + homeRecommendBean.getS_original_price());
                originalPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;
            
        }
    }
    
    @Override
    public int getItemViewType(int position)
    {
        switch (position)
        {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            default:
                return 3;
        }
    }
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        switch (viewType)
        {
            case 0:
                return ViewHolder.get(mContext, parent, R.layout.banner_view, viewType);
            case 1:
                return ViewHolder.get(mContext, parent, R.layout.shopping_button, viewType);
            case 2:
                return ViewHolder.get(mContext, parent, R.layout.shpping_cen_img, viewType);
            case 3:
                return ViewHolder.get(mContext, parent, R.layout.shpping_item, viewType);
            
        }
        return null;
    }
    
}
